package cn.itsource.controller;

import cn.itsource.domain.Teacher;
import cn.itsource.service.ITeacherService;
import cn.itsource.util.ExcelUtil;
import cn.itsource.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 老师controller
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;

    /**
     * 新增和修改
     * @param teacher 老师对象，如果有id则修改，否则是新增
     * @return
     */
    @PostMapping
    public JSONResult<Teacher> save(@RequestBody Teacher teacher){// @RequestBody该注解是要求前端传递的参数必须是一个json对象
        try{
            teacherService.save(teacher);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail(e.getMessage());
        }
    }


    /**
     * 下载教师模板
     */
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        //到处
        ExcelUtil.exportExcel(new ArrayList<Teacher>(),
                "老师列表","老师",Teacher.class,"老师列表",response);
    }

    /**
     * 导出数据(以excel的方式)
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        ExcelUtil.exportExcel(teacherService.getAll(),
                "老师列表","老师",Teacher.class,"老师列表",response);
    }

    /**
     * 上传附件
     * @return
     */
    @PostMapping("/importExcel")
    public JSONResult<Teacher> importExcel(MultipartFile file) throws IOException {
        try{
            //导入excel获取excel中的基本信息
            List<Teacher> teachers = ExcelUtil.importExcel(file, 1, 1, Teacher.class);
            teacherService.batchInsert(teachers);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail("导入失败：" + e.getMessage());
        }
    }

    /**
     * 根据传递的id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JSONResult<Teacher> delete(@PathVariable("id")Long id){
        try{
            teacherService.delete(id);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail("删除失败,原因为:"+e.getMessage());
        }
    }

    /**
     * 根据传递的id查询老师对象
     * @param id  主键id
     * @return
     */
    @GetMapping("/{id}")
    public JSONResult<Teacher> get(@PathVariable("id")Long id){
        try{

            return JSONResult.ok(teacherService.get(id));
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail("查询失败,原因为:"+e.getMessage());

        }
    }

    /**
     * 查询所有的老师列表
     * @return
     */
    @PatchMapping
    public JSONResult<Teacher> getAll(){
        try{
            System.out.println("目标方法。。。。。。。");
            return JSONResult.ok(teacherService.getAll());
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.fail();
        }
    }

}
